03. Our Objects, Your Properties
Prepare
Below, we've picked some object types that are actually used in the Pirate Ship app. Note, some of these types don't exist in our conceptual model and that is fine. Sometimes conceptual models are just that—concepts!
GridSizeGridLocationShip
Specifically, each of these objects are represented as structs.
Reflect
QUESTION:
By looking only at the names of the structs, I want you to enter some properties you think each contains. Formatting your answer is not important here… you can enter a list of names or even write actual Swift code. Whatever you choose, be explicit about the types of each property and whether they are variable or constant. Also, don’t be afraid to be creative with the properties—sometimes you have to think outside of the box!
| GridSize | GridLocation | Ship |
|---|---|---|
ANSWER:
Thanks for giving it some thought! Read on for the properties included in the example app.
We’ll start with the GridSize struct.
struct GridSize {
let width: Int
let height: Int
}
This struct is very simple. It allows us to represent the size of the grid, and we just use two integer properties: width and height. For example, the default game grid has a height and width of 8. But, if we wanted, we could always make the grid another rectangular size. Each of these properties are also constants because during gameplay, the size of the grid does not change.
The next struct is GridLocation.
struct GridLocation {
let x: Int
let y: Int
}
Like GridSize, GridLocation is simple and contains only two integer properties — x and y. Each of these properties are constant and represent the coordinates of a grid square. Instances of this struct are used when we guess grid locations and to represent the location of objects in the grid like mines and ships.
A GridLocation represents the x- and y-coordinates of a grid square.
The final struct is Ship. This is the most complex struct of the three.
struct Ship {
let length: Int
let location: GridLocation
let isVertical: Bool
}
This struct contains basic properties: an integer called length, a bool called isVertical, and a GridLocation called location. All three of these properties are constant. The length and isVertical properties are used to describe the size and orientation of a ship. For example, if a ship has a length of 4, isVertical is true, and its location has an x and y of 0, then the ship is placed vertically starting at (0,0).
This ship is placed at a location of (0,0) in a vertical orientation with a length of 4.
Hence, the location property gives us the starting (x,y) position where the starting position is the top-most or left-most end of a ship. So, if we have another ship with a length of 4, isVertical set to false, and a location where x is 0 and y is 0, then it would be placed here on the grid.
This ship is placed at a location of (0,0) in a horizontal orientation with a length of 4.